package com.cloudpolice.equipment.controller;

import com.cloudpolice.equipment.model.EquipmentModel;
import com.cloudpolice.equipment.model.EquipmentSiteModel;
import com.cloudpolice.equipment.model.InPutParam.EquipmentSelectInParam;
import com.cloudpolice.equipment.service.EquipmentParamService;
import com.cloudpolice.equipment.service.EquipmentService;
import com.cloudpolice.equipment.service.EquipmentSiteService;
import com.cloudpolice.equipment.unit.GetPropertiesClass;
import com.cloudpolice.equipment.unit.RedisOperator;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lh.model.LogResultModel;
import lh.model.ResultVO;
import lh.units.ResultStruct;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author ：梁昊，后端工程师：卢春龙
 * @create 2020-02-29 17:16
 * @function
 * @editLog
 */
@RestController
@RequestMapping("/equipmentController")
@Api(value = "设备粒子层", description = "设备基本信息")
public class EquipmentController {
    @Autowired
    EquipmentService equipmentService;
    @Autowired
    EquipmentParamService equipmentParamService;
    @Autowired
    EquipmentSiteService equipmentSiteService;

    @Autowired
    RedisOperator redisOperator;
    @Autowired
    GetPropertiesClass getPropertiesClass;

    private final byte days = 1;

    /**
     * 通用查询设备基本信息，方法ID：SE20200420164459933B085831E7423
     *
     * @param id 表inf_equipment,字段名id:设备SN号(不可重复主键)
     * @param equipmentName 表inf_equipment,字段名equipmentName:设备名称
     * @param equipmentType 表inf_equipment,字段名equipmentType:设备类型,1:门禁机,2:摄像头
     * @param mac 表inf_equipment,字段名mac:设备的mac地址
     * @param app 表inf_equipment,字段名app:软件名
     * @param version 表inf_equipment,字段名version:软件版本
     * @param ip 表inf_equipment,字段名ip:设备ip
     * @param protocol 表inf_equipment,字段名protocol:通讯协议类型,1:ftp,2:http,3:https
     * @param createAuthor 表inf_equipment,字段名createAuthor:创建者ID
     * @param updateAuthor 表inf_equipment,字段名updateAuthor:更新者ID
     * @param remark 表inf_equipment,字段名remark:备注
     * @param page 当前页数
     * @param limit 每页条数
     * @return 结果
     */
    @ApiOperation(value = "通用查询设备基本信息,方法ID：SE20200420164459933B085831E7423", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String"),
            @ApiImplicitParam(name = "equipmentName", value = "设备名称", dataType = "String"),
            @ApiImplicitParam(name = "equipmentType", value = "设备类型,1:门禁机,2:摄像头", dataType = "String"),
            @ApiImplicitParam(name = "mac", value = "设备的mac地址", dataType = "String"),
            @ApiImplicitParam(name = "app", value = "软件名", dataType = "String"),
            @ApiImplicitParam(name = "version", value = "软件版本", dataType = "String"),
            @ApiImplicitParam(name = "ip", value = "设备ip", dataType = "String"),
            @ApiImplicitParam(name = "protocol", value = "通讯协议类型,1:ftp,2:http,3:https", dataType = "String"),
            @ApiImplicitParam(name = "createAuthor", value = "创建者ID", dataType = "String"),
            @ApiImplicitParam(name = "updateAuthor", value = "更新者ID", dataType = "String"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "String"),
            @ApiImplicitParam(name = "page", value = "当前页数", dataType = "Integer"),
            @ApiImplicitParam(name = "limit", value = "每页条数", dataType = "Integer")
    })
    @PostMapping("/getEquipmentAll")
    public PageInfo getEquipmentAll(@RequestParam(value = "id", required = false) String id
            , @RequestParam(value = "equipmentName", required = false) String equipmentName
            , @RequestParam(value = "equipmentType", required = false) Integer equipmentType
            , @RequestParam(value = "mac", required = false) String mac
            , @RequestParam(value = "app", required = false) String app
            , @RequestParam(value = "version", required = false) String version
            , @RequestParam(value = "ip", required = false) String ip
            , @RequestParam(value = "protocol", required = false) Integer protocol
            , @RequestParam(value = "createAuthor", required = false) String createAuthor
            , @RequestParam(value = "updateAuthor", required = false) String updateAuthor
            , @RequestParam(value = "remark", required = false) String remark
            , @RequestParam(value = "page", defaultValue = "1") int page
            , @RequestParam(value = "limit", defaultValue = "10") int limit) {
        id = id == null ? id : id.trim();
        equipmentName = equipmentName == null ? equipmentName : equipmentName.trim();
        mac = mac == null ? mac : mac.trim();
        app = app == null ? app : app.trim();
        version = version == null ? version : version.trim();
        ip = ip == null ? ip : ip.trim();
        createAuthor = createAuthor == null ? createAuthor : createAuthor.trim();
        updateAuthor = updateAuthor == null ? updateAuthor : updateAuthor.trim();
        remark = remark == null ? remark : remark.trim();

        EquipmentSelectInParam equipmentSelectInParam = new EquipmentSelectInParam();
        equipmentSelectInParam.setId(id);
        equipmentSelectInParam.setEquipmentName(equipmentName);
        equipmentSelectInParam.setEquipmentType(equipmentType);
        equipmentSelectInParam.setMac(mac);
        equipmentSelectInParam.setApp(app);
        equipmentSelectInParam.setVersion(version);
        equipmentSelectInParam.setIp(ip);
        equipmentSelectInParam.setProtocol(protocol);
        equipmentSelectInParam.setCreateAuthor(createAuthor);
        equipmentSelectInParam.setUpdateAuthor(updateAuthor);
        equipmentSelectInParam.setRemark(remark);

        PageHelper.startPage(page, limit);
        List<EquipmentModel> equipmentModels = equipmentService.getEquipmentAll(equipmentSelectInParam);
        PageInfo pageInfo = new PageInfo<>(equipmentModels, limit);
        return pageInfo;
    }


    /**
     * 新增设备基本信息，方法ID：IN2020042015343753521F6BAD246E2
     *
     * @param id 设备SN号(不可重复主键)
     * @param equipmentName 设备名称
     * @param equipmentType 设备类型,1:门禁机,2:摄像头
     * @param mac 设备的mac地址
     * @param app 软件名
     * @param version 软件版本
     * @param ip 设备ip
     * @param protocol 通讯协议类型,1:ftp,2:http,3:https
     * @param createAuthor 创建者ID
     * @param updateAuthor 更新者ID
     * @param stopSign 是否停用
     * @param remark 备注
     * @return 结果
     */
    @ApiOperation(value = "新增设备基本信息,方法ID：IN2020042015343753521F6BAD246E2", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String", required = true)
            , @ApiImplicitParam(name = "equipmentName", value = "设备名称", dataType = "String", required = true)
            , @ApiImplicitParam(name = "equipmentType", value = "设备类型,1:门禁机,2:摄像头", dataType = "String", required = true)
            , @ApiImplicitParam(name = "mac", value = "设备的mac地址", dataType = "String")
            , @ApiImplicitParam(name = "app", value = "软件名", dataType = "String", required = true)
            , @ApiImplicitParam(name = "version", value = "软件版本", dataType = "String", required = true)
            , @ApiImplicitParam(name = "ip", value = "设备ip", dataType = "String", required = true)
            , @ApiImplicitParam(name = "protocol", value = "通讯协议类型,1:ftp,2:http,3:https", dataType = "String", required = true)
            , @ApiImplicitParam(name = "createAuthor", value = "创建者ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "updateAuthor", value = "更新者ID", dataType = "String", required = true)
            , @ApiImplicitParam(name = "stopSign", value = "是否停用", dataType = "Boolean", required = true)
            , @ApiImplicitParam(name = "remark", value = "备注", dataType = "String")
    })
    @PostMapping("/insertEquipment")
    public ResultVO insertEquipment(@RequestParam(value = "id") String id
            , @RequestParam(value = "equipmentName") String equipmentName
            , @RequestParam(value = "equipmentType", defaultValue = "1") int equipmentType
            , @RequestParam(value = "mac", required = false) String mac
            , @RequestParam(value = "app") String app
            , @RequestParam(value = "version") String version
            , @RequestParam(value = "ip") String ip
            , @RequestParam(value = "protocol", defaultValue = "0") int protocol
            , @RequestParam(value = "createAuthor") String createAuthor
            , @RequestParam(value = "updateAuthor") String updateAuthor
            , @RequestParam(value = "stopSign", defaultValue = "0") boolean stopSign
            , @RequestParam(value = "remark", required = false) String remark) {
        equipmentName = equipmentName == null ? equipmentName : equipmentName.trim();
        mac = mac == null ? mac : mac.trim();
        app = app == null ? app : app.trim();
        version = version == null ? version : version.trim();
        ip = ip == null ? ip : ip.trim();
        createAuthor = createAuthor == null ? createAuthor : createAuthor.trim();
        updateAuthor = updateAuthor == null ? updateAuthor : updateAuthor.trim();
        remark = remark == null ? remark : remark.trim();

        EquipmentModel equipmentModel = new EquipmentModel();
        equipmentModel.setId(id);
        equipmentModel.setEquipmentName(equipmentName);
        equipmentModel.setEquipmentType(equipmentType);
        equipmentModel.setMac(mac);
        equipmentModel.setApp(app);
        equipmentModel.setVersion(version);
        equipmentModel.setIp(ip);
        equipmentModel.setProtocol(protocol);
        equipmentModel.setCreateAuthor(createAuthor);
        equipmentModel.setUpdateAuthor(updateAuthor);
        equipmentModel.setStopSign(stopSign);
        equipmentModel.setRemark(remark);
        int repetitionCount = equipmentService.insertEquipmentBeforeCheck(equipmentModel);
        if (repetitionCount > 0)
            return ResultStruct.error("MAC地址重复！", ResultVO.class, int.class);
        int resultCount = equipmentService.insertEquipment(equipmentModel);
        if (resultCount > 0)
            return ResultStruct.success(resultCount);
        else
            return ResultStruct.error("增加失败", ResultVO.class, int.class);
    }

    /**
     * 修改设备基本信息，方法ID：UP20200420161237521E51733D6EA31
     *
     * @param id 设备SN号(不可重复主键)
     * @param equipmentName 设备名称
     * @param equipmentType 设备类型,1:门禁机,2:摄像头
     * @param mac 设备的mac地址
     * @param app 软件名
     * @param version 软件版本
     * @param ip 设备ip
     * @param protocol 通讯协议类型,1:ftp,2:http,3:https
     * @param createAuthor 创建者ID
     * @param updateAuthor 更新者ID
     * @param stopSign 是否停用
     * @param remark 备注
     * @return 结果
     */
    @ApiOperation(value = "修改设备基本信息,方法ID：UP20200420161237521E51733D6EA31", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String",required = true)
            , @ApiImplicitParam(name = "equipmentName", value = "设备名称", dataType = "String")
            , @ApiImplicitParam(name = "equipmentType", value = "设备类型,1:门禁机,2:摄像头", dataType = "String")
            , @ApiImplicitParam(name = "mac", value = "设备的mac地址", dataType = "String")
            , @ApiImplicitParam(name = "app", value = "软件名", dataType = "String")
            , @ApiImplicitParam(name = "version", value = "软件版本", dataType = "String")
            , @ApiImplicitParam(name = "ip", value = "设备ip", dataType = "String")
            , @ApiImplicitParam(name = "protocol", value = "通讯协议类型,1:ftp,2:http,3:https", dataType = "String")
            , @ApiImplicitParam(name = "createAuthor", value = "创建者ID", dataType = "String")
            , @ApiImplicitParam(name = "updateAuthor", value = "更新者ID", dataType = "String")
            , @ApiImplicitParam(name = "stopSign", value = "是否停用", dataType = "Boolean")
            , @ApiImplicitParam(name = "remark", value = "备注", dataType = "String")
    })
    @PostMapping("/updateEquipment")
    public ResultVO updateEquipment(@RequestParam(value = "id") String id
            , @RequestParam(value = "equipmentName",required = false) String equipmentName
            , @RequestParam(value = "equipmentType",required = false) Integer equipmentType
            , @RequestParam(value = "mac",required = false) String mac
            , @RequestParam(value = "app",required = false) String app
            , @RequestParam(value = "version",required = false) String version
            , @RequestParam(value = "ip",required = false) String ip
            , @RequestParam(value = "protocol",required = false) Integer protocol
            , @RequestParam(value = "createAuthor",required = false) String createAuthor
            , @RequestParam(value = "updateAuthor",required = false) String updateAuthor
            , @RequestParam(value = "stopSign",required = false) boolean stopSign
            , @RequestParam(value = "remark",required = false) String remark) {
        id = id == null ? id : id.trim();
        equipmentName = equipmentName == null ? equipmentName : equipmentName.trim();
        mac = mac == null ? mac : mac.trim();
        app = app == null ? app : app.trim();
        version = version == null ? version : version.trim();
        ip = ip == null ? ip : ip.trim();
        createAuthor = createAuthor == null ? createAuthor : createAuthor.trim();
        updateAuthor = updateAuthor == null ? updateAuthor : updateAuthor.trim();
        remark = remark == null ? remark : remark.trim();

        EquipmentModel equipmentModel = new EquipmentModel();
        equipmentModel.setId(id);
        equipmentModel.setEquipmentName(equipmentName);
        equipmentModel.setEquipmentType(equipmentType);
        equipmentModel.setMac(mac);
        equipmentModel.setApp(app);
        equipmentModel.setVersion(version);
        equipmentModel.setIp(ip);
        equipmentModel.setProtocol(protocol);
        equipmentModel.setCreateAuthor(createAuthor);
        equipmentModel.setUpdateAuthor(updateAuthor);
        equipmentModel.setStopSign(stopSign);
        equipmentModel.setRemark(remark);
        int updateCount = equipmentService.updateEquipment(equipmentModel);
        if (updateCount > 0)
            return ResultStruct.success(updateCount);
        else
            return ResultStruct.error("修改失败", ResultVO.class, int.class);
    }


    /**
     * 根据id删除基本信息，方法ID：DE202004201627464593BB02A38C5AD
     *
     * @param id 设备SN号(不可重复主键)
     * @return 结果
     */
    @ApiOperation(value = "根据id删除基本信息,方法ID：DE202004201627464593BB02A38C5AD", notes = "结果")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String", required = true)
    })
    @PostMapping("/DeleteEquipmentById")
    public ResultVO DeleteEquipmentById(@RequestParam(value = "id") String id) {
        id = id == null ? id : id.trim();

        int updateCount = equipmentService.DeleteEquipmentById(id);
        if (updateCount > 0){
            //同时删除设备相关联的参数和位置信息
            int paramCount = equipmentParamService.insertEquipmentParamDefaultBeforeCheck(id);
            if (paramCount > 0) {
                equipmentParamService.deleteEquipmentParamBySn(id);
            }
            int siteCount = equipmentSiteService.insertEquipmentSiteBeforeCheck(id);
            if (siteCount > 0) {
                equipmentSiteService.deleteEquipmentSiteBySn(id);
            }
            return ResultStruct.success(updateCount);}
        else
            return ResultStruct.error("删除失败", ResultVO.class, int.class);
    }

    /**
     * 根据设备SN号得到设备位置信息，方法ID：SE20200229174501655E1C1FD128029
     *
     * @param sn 表vou_EquipmentSite,字段名sn
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据设备SN号得到设备位置信息,方法ID：SE20200229174501655E1C1FD128029", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备SN号(不可重复主键)", dataType = "String", required = true, defaultValue = "sn123456"),
    })
    @PostMapping("/SelectEquipmentSiteBySn")
    public ResultVO SelectEquipmentSiteBySn(@RequestParam(value = "sn") String sn) {
        sn = sn == null ? sn : sn.trim();
        final List<EquipmentSiteModel> equipmentSiteModels = equipmentService.SelectEquipmentSiteBySn(sn);
        if (equipmentSiteModels != null && !equipmentSiteModels.isEmpty()) {
            return ResultStruct.success(equipmentSiteModels, equipmentSiteModels.size());
        } else
            return ResultStruct.error("无此设备位置信息", ResultVO.class);
    }


    /**
     * 根据设备SN号得到设备基本信息，方法ID：SE2020022916211515599A6570123BF
     *
     * @param id       表inf_equipment,字段名id:设备SN号(不可重复主键)
     * @param stopSign 表inf_equipment,字段名stopSign:是否停用
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据设备SN号得到设备基本信息,方法ID：SE2020022916211515599A6570123BF", notes = "设备基本信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String", required = true, defaultValue = "sn123456"),
            @ApiImplicitParam(name = "stopSign", value = "是否停用", dataType = "Boolean", defaultValue = "false")
    })
    @PostMapping("/SelectEquipmentBySn")
    public ResultVO SelectEquipmentBySn(@RequestParam(value = "id") String id
            , @RequestParam(value = "stopSign", required = false, defaultValue = "0") boolean stopSign) {
        id = id == null ? id : id.trim();
        EquipmentSelectInParam equipmentSelectInParam = new EquipmentSelectInParam();
        equipmentSelectInParam.setId(id);
        equipmentSelectInParam.setStopSign(stopSign);
        final EquipmentModel equipmentModel = equipmentService.SelectEquipmentBySn(equipmentSelectInParam);
        if (equipmentModel != null)
            return ResultStruct.success(equipmentModel);
        else
            return ResultStruct.error("无此设备", ResultVO.class);
    }

    /**
     * 根据设备SN号和MAC得到,设备是否允许登录
     *
     * @param id  表inf_equipment,字段名id:设备SN号(不可重复主键)
     * @param mac 表inf_equipment,字段名mac:mac地址
     * @return 设备基本信息
     */
    @ApiOperation(value = "根据设备SN号和MAC得到设备基本信息", notes = "设备是否允许登录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "设备SN号(不可重复主键)", dataType = "String", required = true, defaultValue = "sn123456"),
            @ApiImplicitParam(name = "mac", value = "设备mac(不可重复)", dataType = "String", required = true, defaultValue = "EO-D5-5E-96-89-3B"),
    })
    @PostMapping("/SelectEquipmentBySnAndMac")
    public ResultVO SelectEquipmentBySnAndMac(@RequestParam(value = "id") String id
            , @RequestParam(value = "mac") String mac) {
        id = id == null ? id : id.trim();
        mac = mac == null ? mac : mac.trim();
        EquipmentSelectInParam equipmentSelectInParam = new EquipmentSelectInParam();
        equipmentSelectInParam.setId(id);
        equipmentSelectInParam.setMac(mac);
        equipmentSelectInParam.setStopSign(false);
        final EquipmentModel equipmentModel = equipmentService.SelectEquipmentBySn(equipmentSelectInParam);
        if (equipmentModel != null) {
            String token = redisOperator.writeIntoToken("A005"
                    , equipmentModel.getId()
                    , "equipment", "Android"
                    , getPropertiesClass.getIsAutoAgain());
            LogResultModel logResultModel = this.getLogResultModel(
                    equipmentModel.getId()
                    , "A005"
                    , "Android"
                    , "equipment"
                    , token
                    , days
                    , (byte) 1);
            return ResultStruct.success(logResultModel);
        } else
            return ResultStruct.error("无此设备", ResultVO.class);
    }
    private LogResultModel getLogResultModel(String id
            , String systemId
            , String clientType
            , String useType
            , String accessToken
            , byte days
            , byte useClass) {
        LogResultModel logResultModel = new LogResultModel(days);
        logResultModel.setSystemId(systemId);
        logResultModel.setUseId(id);
        logResultModel.setUseType(useType);
        logResultModel.setAccessToken(accessToken);
        logResultModel.setClientType(clientType);
        logResultModel.setUseClass(useClass);
        return logResultModel;
    }

}
